package com.aiiter.web.controller.house;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.aiiter.common.core.domain.entity.SysUser;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.aiiter.common.annotation.Log;
import com.aiiter.common.core.controller.BaseController;
import com.aiiter.common.core.domain.AjaxResult;
import com.aiiter.common.enums.BusinessType;
import com.aiiter.house.domain.HouseProperty;
import com.aiiter.house.service.IHousePropertyService;
import com.aiiter.common.utils.poi.ExcelUtil;
import com.aiiter.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 存储房产的基本信息，包括房主信息Controller
 * 
 * @author yaocheng
 * @date 2024-08-13
 */
@RestController
@RequestMapping("/house/property")
public class HousePropertyController extends BaseController
{
    @Autowired
    private IHousePropertyService housePropertyService;

    /**
     * 查询存储房产的基本信息，包括房主信息列表
     */
    @PreAuthorize("@ss.hasPermi('house:property:list')")
    @GetMapping("/list")
    public TableDataInfo list(HouseProperty houseProperty)
    {
        startPage();
        List<HouseProperty> list = housePropertyService.selectHousePropertyList(houseProperty);
        return getDataTable(list);
    }

    @GetMapping("/tree")
    public AjaxResult tree(){
        List<Map<String, String>> maps = housePropertyService.selectHousePropertyListTree();
        return success(maps);
    }
    /**
     * 导出存储房产的基本信息，包括房主信息列表
     */
    @PreAuthorize("@ss.hasPermi('house:property:export')")
    @Log(title = "存储房产的基本信息，包括房主信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HouseProperty houseProperty)
    {
        List<HouseProperty> list = housePropertyService.selectHousePropertyList(houseProperty);
        ExcelUtil<HouseProperty> util = new ExcelUtil<HouseProperty>(HouseProperty.class);
        util.exportExcel(response, list, "房产的基本信息");
    }

    /**
     * 导入模板下载
     * @param response
     */
    @PreAuthorize("@ss.hasPermi('house:property:export')")
    @Log(title = "存储房产的基本信息，包括房主信息", businessType = BusinessType.EXPORT)
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        List<HouseProperty> list = new ArrayList<>();
        ExcelUtil<HouseProperty> util = new ExcelUtil<HouseProperty>(HouseProperty.class);
        util.exportExcel(response, list, "房产的基本信息");
    }

    @Log(title = "用户管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:user:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<HouseProperty> util = new ExcelUtil<HouseProperty>(HouseProperty.class);
        List<HouseProperty> housePropertyList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = housePropertyService.importUser(housePropertyList, updateSupport, operName);
        return success(message);
    }

    /**
     * 获取存储房产的基本信息，包括房主信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('house:property:query')")
    @GetMapping(value = "/{propertyId}")
    public AjaxResult getInfo(@PathVariable("propertyId") Long propertyId)
    {
        return success(housePropertyService.selectHousePropertyByPropertyId(propertyId));
    }

    /**
     * 新增存储房产的基本信息，包括房主信息
     */
    @PreAuthorize("@ss.hasPermi('house:property:add')")
    @Log(title = "存储房产的基本信息，包括房主信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HouseProperty houseProperty)
    {
        return toAjax(housePropertyService.insertHouseProperty(houseProperty));
    }

    /**
     * 修改存储房产的基本信息，包括房主信息
     */
    @PreAuthorize("@ss.hasPermi('house:property:edit')")
    @Log(title = "存储房产的基本信息，包括房主信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HouseProperty houseProperty)
    {
        return toAjax(housePropertyService.updateHouseProperty(houseProperty));
    }

    /**
     * 删除存储房产的基本信息，包括房主信息
     */
    @PreAuthorize("@ss.hasPermi('house:property:remove')")
    @Log(title = "存储房产的基本信息，包括房主信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{propertyIds}")
    public AjaxResult remove(@PathVariable String[] propertyIds)
    {
        return toAjax(housePropertyService.deleteHousePropertyByPropertyIds(propertyIds));
    }
}
